Skip to content

Conversation

Darksonn
Copy link
Contributor

@Darksonn Darksonn commented Jul 29, 2025

This is a follow-up to rust-lang/rust#137728.

Note that we only removed coercions, which is what relied on the layout matching. Tuple types with unsized last field are still legal in the language:

use core::ptr::slice_from_raw_parts;

fn main() {
    let array = [1u8; 20];

    let ptr = slice_from_raw_parts(array.as_ptr(), 20 - size_of::<i32>());
    println!("{:?}", unsafe { &*(ptr as *const (i32, [u8])) });
}
(16843009, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])

playground

One way to rationalize this is that it's not different from the fact that these two types might have incompatible layouts:

// A and B may have incompatible layouts

struct A(i32, [u8; 20]);
struct B(i32, [u8]);

@rustbot rustbot added the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Jul 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: The marked PR is awaiting review from a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants